﻿/*===============================================================================
	TASK 2:                                             {Multidimentional Arrays}
	Write a program that reads a rectangular matrix of size N x M and finds in it
    the square 3 x 3 that has maximal sum of its elements
================================================================================*/

using System;

class MaximalSum3x3
{
    static void Main()
    {
        //Console.Write("Enter number of rows=");
        //int rows = int.Parse(Console.ReadLine());

        //Console.Write("Enter number of cols=");
        //int cols = int.Parse(Console.ReadLine());

        int n = 5;
        int m = 7;

        int[,] matrix = new int[,]
        {
            {0,   2,  3,   4,  5,  6,  7},
            {8,   9, 10,  11, 12, 13, 14},
            {15, 16, 17,  18, 19, 20, 21},
            {22, 23, 24,  25, 26, 27, 28},
            {-1, -2, -3,  -4, -5, -6, -7}
        };

        int maxSquare = int.MinValue;
        int currentSquare = int.MinValue;
        int startRow=0;
        int startCol=0;

        for (int row = 0; row < n - 2; row++)
        {
            for (int col = 0; col < m - 2; col++)
            {
                currentSquare = matrix[row, col]     + matrix[row, col + 1]     + matrix[row, col + 2] +
                                matrix[row + 1, col] + matrix[row + 1, col + 1] + matrix[row + 1, col + 2] +
                                matrix[row + 2, col] + matrix[row + 2, col + 1] + matrix[row + 2, col + 2];

                if (currentSquare > maxSquare)
                {
                    maxSquare = currentSquare;
                    startRow = row;
                    startCol = col;
                }
            }
        }

        //Print full matrix
        for (int row = 0; row < n; row++)
        {
            for (int col = 0; col < m; col++)
            {
                Console.Write(matrix[row, col] + " ");
            }
            Console.WriteLine();
        }

        Console.WriteLine();

        //Print maximal square 3x3 from matrix
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                
                Console.Write(matrix[startRow+i, startCol+j] + " ");
            }
            Console.WriteLine();
        }
    }
}

